import { Injectable } from "@angular/core";
import { Headers } from "@angular/http";
import { CmHttp } from "CmHttp";
import { Subject } from "rxjs/Subject";
import { ReplaySubject } from "rxjs/ReplaySubject";
import { Observable } from "rxjs/Observable";
import { APISURL } from "shared";
import { UserService } from "./../user/index";

@Injectable()
export class CommentsService {
  appAgent: string;
  headers = new Headers();

  constructor(
    private http: CmHttp
  ) {
    // let elec  = require('electron');
    // if (elec && elec.remote) {
    //   this.appAgent = elec.remote.getGlobal("appAgent");
    // }
    // this.headers.append("User-Agent",this.appAgent);
  }
  
  /**
   * 获取学战法列表
   */
  getXZFArticles() {
    let url =  `${APISURL}/articles/latest/xuezhanfa`;
    return this.http.get(url, { headers: this.headers, withCredentials: true }, {cacheAge: this.http.CACHE_AGE.Moment})
      .catch(error => {
        this.alertMessage();
        console.info("get XZFArticleList err: ", error);
        return Observable.throw(error);
      });
  }

  /**
   * 获取大盘分析列表
   */
  getDPFXArticles() {
    let url = `${APISURL}/articles/latest/dapanfenxi`;
    return this.http.get(url, { headers: this.headers, withCredentials: true }, {cacheAge: this.http.CACHE_AGE.Moment})
      .catch(error => {
        this.alertMessage();
        console.log("get DPFXArticleList err:", error );
        return Observable.throw(error);
      })
  }

  /**
   * 获取大盘分析全部列表
   */
  getDPFXArticlesOfAllCategory() {
    let url =  `${APISURL}/articles/latest/dapanfenxi?grouping=0`;
    return this.http.get(url, { headers: this.headers, withCredentials: true }, {cacheAge: this.http.CACHE_AGE.Moment})
      .catch(error => {
        this.alertMessage();
        console.info("get DPFXColumn err: ", error);
        return Observable.throw(error);
      });
  }

  /**
   * 获取热门视频课程
   */
  getHotVoide() {
    let url =  `${APISURL}/articles/hot/xuezhanfa`;
    return this.http.get(url, { headers: this.headers, withCredentials: true }, {cacheAge: this.http.CACHE_AGE.Moment})
      .catch(error => {
        this.alertMessage();
        console.info("get hot voide:", error);
        return Observable.throw(error);
      });
  }

  /**
   * 获取下一页
   * @param startTime 时间戳
   * @param limit 条数
   * @param category 二级栏目
   * @param tabNo 一级类别
   */
  getArticleList( startTime: string, limit: number, category: string, tabNo: string) {
    let url =  `${APISURL}/articles/${category}?startTime=${startTime}&limit=${limit}&tabNo=${tabNo}`;
    return this.http.get(url, { headers: this.headers, withCredentials: true }, {cacheAge: this.http.CACHE_AGE.Moment})
    .catch(error => {
      this.alertMessage();
      console.info("get ArticleList err: ", error);
      return Observable.throw(error);
    });
  }

  /**
   * 获取广告
   */
  getAds() {
    let url = `${APISURL}/feeds/ad`;
    return this.http.get(url, { headers: this.headers, withCredentials: true }, {cacheAge: this.http.CACHE_AGE.Moment})
      .catch(error => {
        this.alertMessage();
        console.info("get Ad err:", error);
        return Observable.throw(error);
      });
  }

  private alertMessage() {
    alert("服务连接超时，请稍后重试！");
  }

}
